package gases.as3.librarys.components.swf
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLStream;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	import gases.as3.librarys.components.events.LoadDataEvent;
	import gases.as3.librarys.components.resource.MacroResourceManagaers;
	import gases.as3.librarys.components.skin.SkinStylesMangaer;
	import gases.as3.librarys.components.skin.SkinTransform;
	import gases.as3.librarys.components.skin.data.BaseSkin;
	import gases.as3.librarys.components.skin.transform.TransformXML;

	public class StyleConfig extends EventDispatcher 
	{
		private static var _instance:StyleConfig;
		
		public function StyleConfig()
		{
			init();
		}
		
		/**
		 *获得单例 
		 * @return StyleConfig
		 * 
		 */		
		public static function get instance():StyleConfig
		{
			if(!_instance)
			{
				_instance = new StyleConfig();
			}
			return _instance;
		}
		
		private function init():void
		{
			//加载swf
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			_loader.load(new URLRequest(LoadFileName.SWF_URL));
			
			//加载模块
			_urlStream = new URLStream();
			_urlStream.load(new URLRequest(LoadFileName.MODULE_VIEW_URL));
			_urlStream.addEventListener(Event.COMPLETE, loaderMoudleViewComplete);
		}
		
		private function loaderMoudleViewComplete(e:Event):void
		{
			var stream:URLStream = e.target as URLStream;
			var bytes:ByteArray = new ByteArray();
			stream.readBytes(bytes,0,stream.bytesAvailable);
			bytes.position = 0;
			stream.close();
			var data:Dictionary = new Dictionary();
			while(bytes.bytesAvailable > 0)
			{
				var obj:Object = bytes.readObject();
				var dict:Dictionary = obj as Dictionary;
				for(var key:String in dict)
				{
					data[key] = dict[key];
				}
			}
			this.dispatchEvent(new LoadDataEvent(LoadDataEvent.LOAD_MODULE_DATA_COMPLETE,data))
		}
		
		private function onComplete(e:Event):void
		{
			var swf:DisplayObject = LoaderInfo(e.target).content as DisplayObject;
			MacroResourceManagaers.instance.UIResManager.setUISkinSwf(swf);
			_transform = new SkinTransform();
			_transform.setConfigTransform(new TransformXML());
			//加载皮肤配置
			_configLoader = new URLLoader();
			_configLoader.addEventListener(Event.COMPLETE, onSkinComplete);
			_configLoader.load(new URLRequest(LoadFileName.SKIN_URL));
		}
		
		private function onSkinComplete(e:Event):void
		{
			var xml:XML = new XML(e.target.data);
			var xmlList:XMLList = xml.children();
			for each(var skinXML:XML in xmlList)
			{
				_transform.resloveSkinConfig(skinXML);
			}
			_configLoader.close();
			_configLoader = null;
		}
		
		/**
		 *返回对应的皮肤（通过配置xml里面的style） 
		 * @param name
		 * @return 
		 * 
		 */		
		public function retSkin(name:String):BaseSkin
		{
			var ret:BaseSkin;
			ret =  SkinStylesMangaer.instance.getSkinData(name) as BaseSkin;
			return ret;
		}
		
		private var _baseLoad:URLLoader;
		private var _configLoader:URLLoader;
		private var _loader:Loader;
		private var _transform:SkinTransform;
		private var _urlStream:URLStream;
	}
}